/**
 * 作者：陈文斌 on 2019/1/15.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:crm/constant/constant.dart';
import 'package:crm/view/Dialog/select_image_dialog.dart';

class GridViewAddImage extends StatefulWidget {
  final Color background;
  final EdgeInsets contentPadding;

  @override
  GridViewAddImage({
    Key key,
    this.background = Colors.white,
    this.contentPadding,
  }) : super(key: key);

  State<StatefulWidget> createState() {
    return _GridViewAddImageState();
  }
}

class _GridViewAddImageState extends State<GridViewAddImage> {
  List<Widget> _imageList;

  @override
  void initState() {
    super.initState();
    _imageList = List<Widget>()..add(buildAddButton(context));
  }

  @override
  Widget build(BuildContext context) {
    EdgeInsets _contentPadding =
        widget.contentPadding ?? EdgeInsets.fromLTRB(16, 13, 16, 13);

    return Container(
      color: widget.background,
      padding: _contentPadding,
      child: GridView.count(
        primary: false,
        shrinkWrap: true,
        crossAxisCount: 4,
        children: _imageList.map((item) {
          return item;
        }).toList(),
      ),
    );
  }

  Widget buildAddButton(BuildContext context) {
    return GestureDetector(
      onTap: () {
        print('添加图片');
        showModalBottomSheet(
          context: context,
          builder: (BuildContext sheetContext) {
            return AddImageDialog(
              imageFileCallback: (File imageFile){
                print(imageFile);
                setState(() {
                  _imageList.insert(_imageList.length - 1, buildPhoto(imageFile));
                });
              },
              sheetContext: sheetContext,
            );
          },
        );
      },
      child: Container(
        padding: EdgeInsets.all(8),
        child: Image.asset(
          'images/ic_take_photo.png',
          fit: BoxFit.fill,
        ),
      ),
    );
  }

  Widget buildPhoto(File imageFile) {
    return Stack(
      children: <Widget>[
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: Image.file(
            imageFile,
            fit: BoxFit.fill,
            width: AppScreen.setWidth(375),
            height: AppScreen.setHeight(664),
          ),
        ),
        Positioned(
          top: 0,
          right: 0,
          child: Container(
            width: 16,
            height: 16,
            decoration: BoxDecoration(
              color: AppColors.MainColor,
              borderRadius: BorderRadius.circular(8),
            ),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(8),
              child: MaterialButton(
                onPressed: () {},
                child: Icon(
                  Icons.clear,
                  color: Colors.white,
                  size: 13,
                ),
                padding: EdgeInsets.all(0),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
